package cn.dgiot.fd.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.iot.model.v20180120.BatchQueryDeviceDetailResponse;
import com.aliyuncs.iot.model.v20180120.BatchRegisterDeviceWithApplyIdResponse;
import com.aliyuncs.iot.model.v20180120.DeleteDeviceResponse;
import com.aliyuncs.iot.model.v20180120.DisableThingResponse;
import com.aliyuncs.iot.model.v20180120.EnableThingResponse;
import com.aliyuncs.iot.model.v20180120.InvokeThingServiceResponse;
import com.aliyuncs.iot.model.v20180120.InvokeThingsServiceResponse;
import com.aliyuncs.iot.model.v20180120.QueryDeviceDesiredPropertyResponse;
import com.aliyuncs.iot.model.v20180120.QueryDeviceDetailResponse;
import com.aliyuncs.iot.model.v20180120.QueryDevicePropertyStatusResponse;
import com.aliyuncs.iot.model.v20180120.QueryDeviceResponse;
import com.aliyuncs.iot.model.v20180120.RegisterDeviceResponse;
import com.aliyuncs.iot.model.v20180120.ResetThingResponse;
import com.aliyuncs.iot.model.v20180120.SetDeviceDesiredPropertyResponse;
import com.aliyuncs.iot.model.v20180120.SetDevicePropertyResponse;

import cn.dgiot.fd.model.DeviceModel;
import cn.dgiot.fd.model.PropertiesSetModel;
import cn.dgiot.fd.service.IDeviceService;

/**
 * [设备管理]
 * @author
 */
@RestController
@RequestMapping("/device")
public class DeviceController {

	@Autowired
	private IDeviceService deviceService;
	
	/**
	 * [设备注册]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/registerDevice")
	public String registerDevice(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		RegisterDeviceResponse registerDeviceResponse = deviceService.registerDevice(deviceModel);
		return JSON.toJSONString(registerDeviceResponse);
	}
	
	/**
	 * [删除设备]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/deleteDevice")
	public String deleteDevice(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		DeleteDeviceResponse deleteDeviceResponse = deviceService.deleteDevice(deviceModel);
		return JSON.toJSONString(deleteDeviceResponse);
	}
	
	/**
	 * [修改设备]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/updateDevice")
	public String updateDevice(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		RegisterDeviceResponse registerDeviceResponse = deviceService.updateDevice(deviceModel);
		return JSON.toJSONString(registerDeviceResponse);
	}
	
	/**
	 * [查询单个设备详情]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/queryDeviceDetail")
	public String queryDeviceDetail(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		QueryDeviceDetailResponse queryDeviceDeailResponse = deviceService.queryDeviceDetail(deviceModel);
		return JSON.toJSONString(queryDeviceDeailResponse);
	}
	
	/**
	 * [批量查询设备详情]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/batchQueryDeviceDetail")
	public String batchQueryDeviceDetail(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		BatchQueryDeviceDetailResponse batchQueryDeviceDeailResponse = deviceService.batchQueryDeviceDetail(deviceModel);
		return JSON.toJSONString(batchQueryDeviceDeailResponse);
	}
	
	/**
	 * [查询设备列表]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/queryDevice")
	public String queryDevice(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		QueryDeviceResponse queryDeviceResponse = deviceService.queryDevice(deviceModel);
		return JSON.toJSONString(queryDeviceResponse);
	}
	
	/**
	 * [设备停用]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/enableThing")
	public String enableThing(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		EnableThingResponse enableThingResponse = deviceService.enableThing(deviceModel);
		return JSON.toJSONString(enableThingResponse);
	}
	
	/**
	 * [设备启用]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/disableThing")
	public String disableThing(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		DisableThingResponse disableThingResponse = deviceService.disableThing(deviceModel);
		return JSON.toJSONString(disableThingResponse);
	}
	
	/**
	 * [重置指定设备]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/resetThing")
	public String resetThing(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		ResetThingResponse resetThingResponse = deviceService.resetThing(deviceModel);
		return JSON.toJSONString(resetThingResponse);
	}
	
	/**
	 * [设备批量注册]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/batchRegisterDevice")
	public String batchRegisterDevice(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		BatchRegisterDeviceWithApplyIdResponse batchRegisterDeviceWithApplyIdResponse = 
				deviceService.batchRegisterDeviceWithApplyId(deviceModel);
		return JSON.toJSONString(batchRegisterDeviceWithApplyIdResponse);
	}
	
	
	/**
	 * [设备设置属性值（异步）]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/setDeviceProperty")
	public String setDeviceProperty(@RequestBody PropertiesSetModel propertiesSetModel,HttpServletResponse response) {
		SetDevicePropertyResponse setDevicePropertyResponse = deviceService.setDeviceProperty(propertiesSetModel);
		return JSON.toJSONString(setDevicePropertyResponse);
	}
	
	/**
	 * [批量设备设置属性值（异步）]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/setDevicesProperty")
	public String setDevicesProperty(@RequestBody PropertiesSetModel propertiesSetModel,HttpServletResponse response) {
		SetDevicePropertyResponse setDevicePropertyResponse = deviceService.setDevicesProperty(propertiesSetModel);
		return JSON.toJSONString(setDevicePropertyResponse);
	}
	
	/**
	 * [设备调用指定服务（同步）]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/invokeThingService")
	public String invokeThingService(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		InvokeThingServiceResponse invokeThingServiceResponse = deviceService.invokeThingService(deviceModel);
		return JSON.toJSONString(invokeThingServiceResponse);
	}
	
	/**
	 * [设备调用指定服务（批量、同步）]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/invokeThingsService")
	public String invokeThingsService(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		InvokeThingsServiceResponse invokeThingsServiceResponse = deviceService.invokeThingsService(deviceModel);
		return JSON.toJSONString(invokeThingsServiceResponse);
	}
	
	/**
	 * [修改设备属性值]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/setDeviceDesiredProperty")
	public String setDeviceDesiredProperty(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		SetDeviceDesiredPropertyResponse setDeviceDesiredPropertyResponse = deviceService.setDeviceDesiredProperty(deviceModel);
		return JSON.toJSONString(setDeviceDesiredPropertyResponse);
	}
	
	/**
	 * [查询指定设备的期望属性值]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/queryDeviceDesiredProperty")
	public String queryDeviceDesiredProperty(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		QueryDeviceDesiredPropertyResponse queryDeviceDesiredPropertyResponse = deviceService.queryDeviceDesiredProperty(deviceModel);
		return JSON.toJSONString(queryDeviceDesiredPropertyResponse);
	}
	
	/**
	 * [查询指定设备属性快照]
	 * @param deviceModel
	 * @param response
	 * @return
	 */
	@RequestMapping("/queryDevicePropertyStatus")
	public String queryDevicePropertyStatus(@RequestBody DeviceModel deviceModel,HttpServletResponse response) {
		QueryDevicePropertyStatusResponse queryDevicePropertyStatusResponse = deviceService.queryDevicePropertyStatus(deviceModel);
		return JSON.toJSONString(queryDevicePropertyStatusResponse);
	}
	
}
